import os
from enum import Enum
# path = "/home/qzr/gitee/Boost_Engine/asset"; 

class Document:
    def __init__(self, title, body, url):
        self.title = title
        self.body = body
        self.url = url

class Retriever:
    def __init__(self,path = "/home/qzr/gitee/Boost_Engine/asset"):
        self.root_path = path
        self.official_path = "http://81.71.17.201:9090"

    def ChangeDir(self,path):
        self.root_path = path

    def Parse(self):
        Docs = []
        for root, dirs, files in os.walk(self.root_path):
            for file in files:
                filepath = os.path.join(root, file)
                if filepath.lower().endswith(".html"):
                    with open(filepath,"r",encoding="utf-8") as content:
                        html = content.read()
                        title = self.ExtractTitle(html)
                        body = self.DiscardLable(html)
                        url = self.official_path + filepath[len(self.root_path):]
                        Docs.append(Document(title,body,url))
        
    def ExtractTitle(self,html) ->str:
        title = ""
        start = html.find("<title>")
        end = html.find("</title>",start + len("<title>"))
        if start == -1 or end == -1 or start > end:
            return title
        return html[start + len("<title>"): end]
    class Status(Enum):
        LABLE = 1
        BODY = 2
    def DiscardLable(self,html) ->str:
        status = self.Status.LABLE
        body = ""
        for c in html:
            if status == self.Status.LABLE:
                if c == '>':
                    status = self.Status.BODY
            else:
                if c == '<':
                    status = self.Status.LABLE
                else:
                    body += c
        return body
    


def WritePure(purePath,docs):
    with open(purePath,"w",encoding="utf-8") as f:
        for doc in docs:
            item = doc.title + '\3' + doc.body + '\3' + doc.url + '\n'
            f.write(item)









        




